<template>
  <c-dialog ref="dialog" :data="selData" single title="产品复尺" @open="openHandle" @closed="closedHandle">
    <c-list ref="form" :model="formData" :rules="formRules" label-width="150px">
      <c-list-item label="装修任务号" :value="detail.decorationTaskCode" />
      <c-list-item label="所属门店" :value="detail.shopName" />
      <c-list-item prop="proRerulerTime" label="产品复尺完成时间">
        <el-date-picker
          v-model="formData.proRerulerTime"
          type="date"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="选择日期"
          clearable
        />
      </c-list-item>
      <c-list-item prop="files" label="产品复尺资料">
        <c-file-list v-model="formData.files" view-type="image" :model-id="modelId" type-code="ACCESS022" />
      </c-list-item>
      <c-list-item>
        <div class="btn-container">
          <c-button @click="cancelHandle">取消</c-button>
          <c-button type="primary" @click="confirmHandle">确认</c-button>
        </div>
      </c-list-item>
    </c-list>
  </c-dialog>
</template>
<script>
import { postTriad } from '@/api/decorate'
import { deepClone } from '@/utils'
export default {
  name: 'Measure',
  components: {
  },
  props: {
    selData: {
      type: Array,
      default: () => []
    }
  },
  data() {
    const validateFiles = (r, v, c) => {
      if (this.$getFiles(v, this.modelId).filter(e => e.dr !== 1 && e.dr !== '1').length === 0) {
        c(new Error('至少上传一个产品复尺资料'))
      } else {
        c()
      }
    }
    return {
      formData: {
        proRerulerTime: '',
        files: []
      },
      formRules: {
        proRerulerTime: [{ required: true, message: '请选择产品复尺完成时间', trigger: 'change' }],
        files: [{ required: true, validator: validateFiles, trigger: 'change' }]
      },
      modelId: ''
    }
  },
  computed: {
    detail() {
      if (this.selData.length > 0) {
        return this.selData[0]
      } else {
        return {}
      }
    }
  },
  methods: {
    confirmHandle() {
      return this.$refs.form.validate().then(() => {
        const data = deepClone(this.selData[0])
        data.proRerulerTime = this.formData.proRerulerTime
        data.files = this.$getFiles(this.formData.files, this.detail.id)
        data.reqType = 'reSize'
        return postTriad(data).then(res => {
          this.$message.success('操作成功！')
          this.$emit('on-success')
          this.$refs.dialog.visible = false
        })
      })
    },
    cancelHandle() {
      this.$refs.dialog.visible = false
    },
    openHandle() {
      this.modelId = this.detail.id
    },
    closedHandle() {
      this.modelId = ''
      this.$refs.form.resetFields()
    }
  }
}
</script>
